package DataBase;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;

import DataObjects.StandardMenuLeftList;
import DataObjects.StandardMenuRightList;

public class GetRecipesToRightListForUpdStandMenu {
	
	private  String noMenu;
	private String flag;
	private Vector<String>  row;
	private Vector<String>  row1;
	private Vector<StandardMenuRightList>  resTable;
	
	public GetRecipesToRightListForUpdStandMenu(String NumberMenu,String flag)
	{
		this.noMenu=NumberMenu;
		this.flag=flag;
	}

	public Vector<StandardMenuRightList> getRecipesOfMenu()
	{
		SQLConnection sc=new SQLConnection();
			
		try {
			
			
			//if menu is standard menu
			
			
				PreparedStatement ps1 = sc.getConnection().prepareStatement("SELECT Rname,isSelectable,price,rquantity,rm.rid,rmishkal From Recipe r,recipe_in_menu rm where rm.Mid=? and rm.Rid=r.Rid");	
				ps1.setString(1,noMenu);
				ResultSet rs=ps1.executeQuery();
				
				
				resTable = new Vector<StandardMenuRightList>();
				
				 while(rs.next())
			        {   
					 
					 StandardMenuRightList leftList = new StandardMenuRightList();
					 leftList.setName(rs.getString(1));
					 leftList.setIsSelectable(rs.getInt(2));
					 leftList.setPrice(( rs.getInt(3)));
					 leftList.setQuantity(rs.getInt(4));
					 leftList.setRid(rs.getInt(5));
					 leftList.setMishkal(rs.getString(6));
					 resTable.add(leftList);
				    }
				 
				 try {
					 
						
						PreparedStatement ps = sc.getConnection().prepareStatement("Select  SUM(price)" +
							                                                     	" FROM   " +
							                                                         	" (Select TRUNCATE(it.iCost*itr.iquantity,1) as price ,itr.iid,it.iCost,itr.iquantity " +
							                                                         	"     From item_in_recipe itr,item it        " +
							                                                             	"  Where itr.rid=? AND itr.iid=it.iid) AS cost");	
						
						PreparedStatement ps2 = sc.getConnection().prepareStatement("SELECT res.rconstPrice FROM recipe res Where res.rid=? ;");
						for(int i=0;i<resTable.size();i++)
						{
									ps.setString(1, Integer.toString( resTable.get(i).getRid()));
									ps2.setString(1, Integer.toString( resTable.get(i).getRid()));
						 
						ResultSet rs2=ps.executeQuery();	
						ResultSet rs1=ps2.executeQuery();	
						
								
								 while(rs2.next() && rs1.next())
							        {   
					                            
									 resTable.get(i).setCost(Float.parseFloat (rs2.getString(1)));	       //--adding  price to details of recipe  
									 resTable.get(i).setConstCost(Float.parseFloat(rs1.getString(1)));           //---adding constPrice(constant) of recipe 
									 				         			        
							        }   //while
						  		
							
					     }   //for
						ps.close();
						ps2.close();
						
						
						
									
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
				 sc.getConnection().close();	 
			
		}
		
		
		
		
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		
		
	
		
		
		return 	resTable;
		
	}
	
	
		
	}